﻿using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using CredentialsManager.Helpers;
using CredentialsManager.Properties;
using CredentialsManager.Common;

namespace CredentialsManager.ViewModels
{
    public class PasswordViewModel : UserViewModel
    {
        #region Private classes

        protected class SetupWorkerInput
        {
            public string Application { get; set; }
            public string UserName { get; set; }
        }

        #endregion

        #region Private member variables

        private BackgroundWorker m_SetupWorker;

        #endregion

        #region Constructor

        public PasswordViewModel(string displayName, string userName,
            string application, PasswordSettings passwordSettings)
            : base(displayName, application, passwordSettings)
        {
            Debug.Assert(this.IsUserNameEditable);
            this.UserName = userName;
            this.IsUserNameEditable = false;

            m_SetupWorker = new BackgroundWorker();
            m_SetupWorker.Initialise(m_SetupWorker_DoWork,
                m_SetupWorker_RunWorkerCompleted, null, false);

            if (passwordSettings.RequiresQuestionAndAnswer)
            {
                var input = new SetupWorkerInput() { Application = application, UserName = userName };
                this.IsLoading = true;
                m_SetupWorker.RunWorkerAsync(input);
            }
        }

        #endregion

        #region Background worker handlers

        private void m_SetupWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var input = e.Argument as SetupWorkerInput;
            Debug.Assert(input != null);

            var proxy = ProxyFactory.Create<PasswordManagerClient, IPasswordManager>(
                ServiceAddressList.CredentialsService);

            try
            {
                string passwordQuestion = proxy.GetPasswordQuestion(
                    input.Application, input.UserName);
                e.Result = passwordQuestion;
            }
            finally
            {
                ProxyFactory.Cleanup<PasswordManagerClient, IPasswordManager>(proxy);
            }
        }

        private void m_SetupWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                this.IsLoading = false;
                ErrorHandler.HandleNonSpecificWCFException(e.Error, this.DisplayName);
            }
            else
            {
                this.IsLoading = false;

                if (e.Result == null)
                {
                    HandleNoPasswordQuestion();
                }
                else
                {
                    this.SecurityQuestion = e.Result.ToString();
                }
            }
        }

        #endregion

        #region Protected methods

        protected virtual void HandleNoPasswordQuestion()
        { }

        #endregion
    }
}
